package com.oa.ylms.apps.business.goods.impl;

import java.util.List;

import com.oa.ylms.apps.business.goods.IGoodsCategoryService;
import com.oa.ylms.apps.business.goods.IGoodsInfoService;
import com.oa.ylms.apps.common.DateUtil;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.dao.goods.IGoodsInfoDao;
import com.oa.ylms.apps.entity.goods.GoodCategoryValue;
import com.oa.ylms.apps.entity.goods.GoodInfoValue;

public class GoodsInfoServiceImpl implements IGoodsInfoService {
	private IGoodsInfoDao infoDao;
	private GoodInfoValue goodInfo;
	private GoodCategoryValue goodCategory;
	private IGoodsCategoryService categoryService;
	
	
	public List<GoodInfoValue> queryAllByCateId(int categoryId, int role)
			throws Exception {
		goodCategory = categoryService.queryByCateId(categoryId);
		return infoDao.queryAllByCateId(goodCategory, role);
	}

	@Override
	public List<GoodInfoValue> queryWithPage(Integer categoryId, Integer states,
			 int rows, int pageNo) throws Exception{
		if(categoryId!=-1){
		goodInfo.setGoodCategory(categoryService.queryByCateId(categoryId));
		}
		if(states!=-1){
		goodInfo.setStates(states);
		}
		return infoDao.queryGoodInfoWithPage(goodInfo, rows, pageNo);
	}

	@Override
	public int countTpages(Integer categoryId, Integer states,int rows) throws Exception {
		
		if(categoryId!=-1){
		goodInfo.setGoodCategory(categoryService.queryByCateId(categoryId));
		}
		if(states!=-1){
		goodInfo.setStates(states);
		}
		int totalPages=infoDao.countTpages(goodInfo, rows);
		
		return totalPages;
	}
	public boolean modifyGoodsInfo(GoodInfoValue info) throws Exception {
		goodInfo = queryByGoodId(info.getGoodId());
		goodInfo.setGoodName(info.getGoodName());
		goodInfo.setDescription(info.getDescription());
		goodInfo.setGoodCategory(info.getGoodCategory());
		goodInfo.setTotal(info.getTotal());
		if(info.getOnStore() != null)
		goodInfo.setOnStore(info.getOnStore());
		goodInfo.setPrice(info.getPrice());
		if(info.getStates()!=null){
		goodInfo.setStates(info.getStates());
		}
		return infoDao.editGoodsInfo(goodInfo);
	}

	public int addGoodsInfo(GoodInfoValue info) throws Exception {
		info.setGoodCategory(categoryService.queryByCateId(info.getGoodCategory().getCategoryId()));
		info.setOnStore(info.getTotal());
		info.setCreateTime(DateUtil.toDate(DateUtil.getCurrentDate()));
		info.setStates(IDEDStates.ENABLE);
		return infoDao.addGoodsInfo(info);
	}

	public GoodInfoValue queryByGoodId(int goodId) throws Exception {
		goodInfo.setGoodId(goodId);
		return infoDao.queryByGoodId(goodInfo);
	}

	public boolean deleteGoodsInfo(int goodId) throws Exception {
		goodInfo = queryByGoodId(goodId);
		goodInfo.setStates(IDEDStates.DELETE);
		return infoDao.editGoodsInfo(goodInfo);
	}


	@Override
	public String deleteGoodInfos(String goodIds) throws Exception {
		String unDelIds = "";
		String[] idArr = goodIds.split(",");
		for (String idStr : idArr) {
			int goodId = Integer.parseInt(idStr);
			goodInfo = queryByGoodId(goodId);
			goodInfo.setStates(IDEDStates.DELETE);
			boolean result = infoDao.editGoodsInfo(goodInfo);
			if (result == false) {
				unDelIds = goodId + ",";
				continue;
			}
		}
		return unDelIds.substring(0, unDelIds.length() - 1);
	}

	@Override
	public boolean toDisable(int goodId) throws Exception {
		goodInfo = queryByGoodId(goodId);
		goodInfo.setStates(IDEDStates.DISABLE);
		return infoDao.editGoodsInfo(goodInfo);
	}

	@Override
	public boolean toEnable(int goodId) throws Exception {
		goodInfo = queryByGoodId(goodId);
		goodInfo.setStates(IDEDStates.ENABLE);
		return infoDao.editGoodsInfo(goodInfo);
	}

	public IGoodsInfoDao getInfoDao() {
		return infoDao;
	}

	public void setInfoDao(IGoodsInfoDao infoDao) {
		this.infoDao = infoDao;
	}

	public GoodInfoValue getGoodInfo() {
		return goodInfo;
	}

	public void setGoodInfo(GoodInfoValue goodInfo) {
		this.goodInfo = goodInfo;
	}

	public GoodCategoryValue getGoodCategory() {
		return goodCategory;
	}

	public void setGoodCategory(GoodCategoryValue goodCategory) {
		this.goodCategory = goodCategory;
	}

	public IGoodsCategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(IGoodsCategoryService categoryService) {
		this.categoryService = categoryService;
	}

	@Override
	public int getTotalPages() {
		return infoDao.getTotalNumber();
	}




}
